package com.qianyu.project.core.context.handler;

import com.qianyu.project.core.scan.FlowHandlerScanRegister;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gs
 * @date create in 2021/1/8 21:20
 */
@Component
public class FlowHandlerManager implements FlowHandlerFactory, InitializingBean {

    @Autowired
    private ApplicationContext context;
    private Map<String, FlowFinishedHandler> handlerFactory;
    private FlowHandlerFactoryBean flowHandlerFactoryBean;

    @Override
    public FlowFinishedHandler getHandler(String type) {
        return handlerFactory.get(type);
    }

    @Override
    public void pushHandler(String key,Class type) {
        FlowFinishedHandler flowFinishedHandler = flowHandlerFactoryBean.doCreateHandler(key, type,context);
        handlerFactory.put(key, flowFinishedHandler);
    }

    @Override
    public Map<String, FlowFinishedHandler> getContext() {
        return this.handlerFactory;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.initializeBean();
    }

    public void initializeBean() {
        this.handlerFactory = new HashMap<>();
        this.flowHandlerFactoryBean = new FlowHandlerFactoryBean();
        String packages = "com.qianyu.project";
        FlowHandlerScanRegister flowHandlerScanRegister = new FlowHandlerScanRegister(this, packages);
    }
}
